package com.atguigu.gulimall.product.service.impl;

import com.atguigu.common.constant.ProductConstant;
import com.atguigu.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimall.product.service.AttrGroupService;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.AttrResVo;
import com.atguigu.gulimall.product.vo.AttrVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.AttrDao;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.service.AttrService;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrGroupService attrGroupService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                new QueryWrapper<AttrEntity>()
        );

        return new PageUtils(page);
    }

    //向attr和attr-group-relation表添加信息
    @Override
    public void saveDetail(AttrVo attrVo) {
        AttrEntity attrEntity = new AttrEntity();
        //向attr表中添加数据
        BeanUtils.copyProperties(attrVo, attrEntity);
        baseMapper.insert(attrEntity);
        //向 AttrAttrgroupRelation中添加数据
        if (attrVo.getAttrType() == ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode() && attrVo.getAttrGroupId() != null) {
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            relationEntity.setAttrId(attrEntity.getAttrId());
            relationEntity.setAttrGroupId(attrVo.getAttrGroupId());
            relationEntity.setAttrSort(0);
            attrAttrgroupRelationService.save(relationEntity);
        }


    }

    //查询列表
    @Override
    public PageUtils queryBaseAttrPage(Map<String, Object> params, Long catelogId, String attrType) {
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper();
        wrapper.eq("attr_type", "base".equalsIgnoreCase(attrType) ? 1 : 0);
        if (catelogId != 0) {
            wrapper.eq("catelog_id", catelogId);
        }
        String key = (String) params.get("key");
        if (StringUtils.isNotEmpty(key)) {
            wrapper.and(queryWrapper ->
                    queryWrapper.eq("attr_id", key).or().like("attr_name", key));
        }
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params), wrapper);
        PageUtils pageUtils = new PageUtils(page);
        List<AttrEntity> records = page.getRecords();
        List<AttrResVo> attrResVos = records.stream().map((attrEntity) -> {
            AttrResVo attrResVo = new AttrResVo();
            BeanUtils.copyProperties(attrEntity, attrResVo);
            //查询CatelogName
            CategoryEntity category = categoryService.getById(attrEntity.getCatelogId());
            if (category != null) {
                String catelogname = category.getName();
                attrResVo.setCatelogName(catelogname);
            }

            //查询GroupName

            if ("base".equalsIgnoreCase(attrType)) {
                AttrAttrgroupRelationEntity relationServiceOne = attrAttrgroupRelationService.getOne(new QueryWrapper<AttrAttrgroupRelationEntity>()
                        .eq("attr_id", attrEntity.getAttrId()));
                if (relationServiceOne != null) {
                    Long groupId = relationServiceOne.getAttrGroupId();
                    AttrGroupEntity attrGroup = attrGroupService.getById(groupId);
                    if (attrGroup != null) {
                        String attrGroupName = attrGroup.getAttrGroupName();
                        attrResVo.setGroupName(attrGroupName);
                    }
                }
            }

            return attrResVo;
        }).collect(Collectors.toList());
        pageUtils.setList(attrResVos);
        return pageUtils;
    }

    //修改时回显所有信息
    @Override
    public AttrResVo getAttrInfo(Long attrId) {
        AttrEntity attrEntity = baseMapper.selectById(attrId);
        AttrResVo attrResVo = new AttrResVo();
        BeanUtils.copyProperties(attrEntity, attrResVo);
        if (attrEntity.getAttrType() == ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode()) {
            //设置attgroupId 和attgroupName
            AttrAttrgroupRelationEntity relationEntityOne = attrAttrgroupRelationService.getOne(new QueryWrapper<AttrAttrgroupRelationEntity>()
                    .eq("attr_id", attrId));
            if (relationEntityOne != null) {
                attrResVo.setAttrGroupId(relationEntityOne.getAttrGroupId());
                AttrGroupEntity attrGroupEntity = attrGroupService.getById(relationEntityOne.getAttrGroupId());
                if (attrGroupEntity != null) {
                    attrResVo.setGroupName(attrGroupEntity.getAttrGroupName());
                }
            }
        }

        //设置分类信息
        Long catelogId = attrEntity.getCatelogId();
        Long[] catelogPath = categoryService.findcatelogPath(catelogId);
        attrResVo.setCatelogPath(catelogPath);


        return attrResVo;
    }

    //  //基本修改和 修改关联分组id
    @Override
    public void updateDetail(AttrVo attrVo) {
        //基本修改
        AttrEntity attrEntity = new AttrEntity();
        BeanUtils.copyProperties(attrVo, attrEntity);
        baseMapper.updateById(attrEntity);
        if (attrEntity.getAttrType() == ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode()) {
            //修改关联分组id
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("attr_id", attrVo.getAttrId());
            int count = attrAttrgroupRelationService.count(wrapper);
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            relationEntity.setAttrGroupId(attrVo.getAttrGroupId());
            relationEntity.setAttrId(attrVo.getAttrId());
            if (count > 0) {

                attrAttrgroupRelationService.update(relationEntity, wrapper);
            } else {
                attrAttrgroupRelationService.save(relationEntity);
            }
        }


    }

    //查询属性和分组的关联
    @Override
    public List<AttrEntity> getRelationAttr(Long attrgroupId) {
        QueryWrapper<AttrAttrgroupRelationEntity> wrapper = new QueryWrapper();
        wrapper.eq("attr_group_id", attrgroupId);
        List<AttrAttrgroupRelationEntity> list = attrAttrgroupRelationService.list(wrapper);
        if (list == null || list.size() == 0) {
            return null;
        }
        List<Long> attrIds = list.stream().map((attr) -> {
            return attr.getAttrId();
        }).collect(Collectors.toList());


        return baseMapper.selectBatchIds(attrIds);
    }
    //在attr中查询检索类型为1的数据
    @Override
    public List<Long> selectSearchAttrIds(List<Long> attrIds) {
        return baseMapper.selectSearchAttrIds(attrIds);

    }

}